$(function() {
//新闻
  $('.news').empty();
  $.ajax({
    url: 'http://v.juhe.cn/toutiao/index?type=top&key=9b965801da7dd6e63c02be6c1a99597b',
    //url:'json/news.json',
    type: 'get',
    dataType: 'json',
    cache: false,
    success:function(data){
      for(var i=0; i<data.result.data.length; i++) {
        $('.news').append('<div class="news_content" hrefs="'+data.result.data[i].url+'"><a href="#pageones" data-transition="slide"><img src="'+data.result.data[i].thumbnail_pic_s+'"/>'+'<div class=title>'+data.result.data[i].title+'<p>作者：'+data.result.data[i].author_name+'<br>'+data.result.data[i].date+'</p></div></a></div>')
      }
    },
    error: function() {
      $('.news').css({
        'padding':'0px',
      })
      $('.news').append('<img src="img/error.png" style="width:100%">')
    }
  })
  //天气
  $('.weather_content1').empty();
  $('.weather_content2').empty();
  $('.weather_content3').empty();
  weather('shenyang');
  life('shenyang');
  future('shenyang');
  // 刷新
  $('.ui-icon-recycle').on("tap",function(){
    location.reload()
  })
  // 浏览器定位城市
  setTimeout(function(){getLocation(),1000});
  function getLocation() {
    var x;
    if (navigator.geolocation) {
      navigator.geolocation.getCurrentPosition(showPosition);
    }
    else{
      x="Geolocation is not supported by this browser.";
    }
  }
  function showPosition(position) {
    x=position.coords.latitude.toFixed(2)+":"+position.coords.longitude.toFixed(2);
    weather(x);
    life(x);
    future(x);
  }
  function weather(loc) {
    $.ajax({
      url:"https://api.seniverse.com/v3/weather/now.json?key=xfv5rncrnafqmek4&location="+loc+"&language=zh-Hans&unit=c",
      // url:'json/daliy.json',
      type:"get",
      dataType:"json",
      cache: false,
      success: function(data) {
       $('.weather_content1').empty();
       $('.weather_content1').append('<img src="img/weather/'+data.results[0].now.code+'.png"><p>'+data.results[0].location.name+'&nbsp;&nbsp;<span>'+data.results[0].now.text+'&nbsp;&nbsp;当前温度：'+data.results[0].now.temperature+'℃</span></p>');
      },
      error: function() {
        $('.weather_content1').empty();
        $('.weather').css({
          'padding':'0px',
        })
        $('.weather_content1').append('<img src="img/error.png" style="width:100%">')
      }
    })
  }
  function life(loc) {
    $.ajax({
      url:"https://api.seniverse.com/v3/life/suggestion.json?key=xfv5rncrnafqmek4&location="+loc+"&language=zh-Hans&unit=c",
      // url:'json/life.json',
      type:"get",
      dataType:"json",
      cache: false,
      success: function(data) {
       $('.weather_content2').empty();
       $('.weather_content2').append('<p>生活指数<a class="ui-btn ui-corner-all ui-icon-heart ui-btn-icon-notext"></a></p><div class="ui-grid-a"><div class="ui-block-a"><span>洗车指数：'+data.results[0].suggestion.car_washing.brief+'</span></div><div class="ui-block-b"><span>穿衣指数：'+data.results[0].suggestion.dressing.brief+'</span></div><div class="ui-block-a"><span>感冒指数：'+data.results[0].suggestion.flu.brief+'</span></div><div class="ui-block-b"><span>运动指数：'+data.results[0].suggestion.sport.brief+'</span></div><div class="ui-block-a"><span>旅游指数：'+data.results[0].suggestion.travel.brief+'</span></div><div class="ui-block-b"><span>紫外线指数：'+data.results[0].suggestion.uv.brief+'</span></div></div>');
      },
      error: function() {
        $('.weather_content2').empty();
        $('.weather').css({
          'padding':'0px',
        })
        $('.weather_content1').append('<img src="img/error.png" style="width:100%">')
      }
    })
  }
  function future(loc) {
    $.ajax({
      url:"https://api.seniverse.com/v3/weather/daily.json?key=xfv5rncrnafqmek4&location="+loc+"&language=zh-Hans&unit=c&start=0&days=5",
      // url:"json/weather.json",
      type:"get",
      dataType:"json",
      cache: false,
      success: function(data) {
       $('.weather_content3').empty();
       $('.weather_content3').append('<p>逐日天气<a class="ui-btn ui-corner-all ui-icon-cloud ui-btn-icon-notext"></a></p><div class="ui-grid-solo"><div class="ui-block-a"><span>今天</span><span>白天：'+data.results[0].daily[0].text_day+'&nbsp;<img src="img/weather/'+data.results[0].daily[0].code_day+'.png">&nbsp;&nbsp;最高气温：'+data.results[0].daily[0].high+'℃</span><span>夜间：'+data.results[0].daily[0].text_night+'&nbsp;<img src="img/weather/'+data.results[0].daily[0].code_night+'.png">&nbsp;&nbsp;最低气温：'+data.results[0].daily[0].low+'℃</span><span>'+data.results[0].daily[0].wind_direction+'风&nbsp;&nbsp;'+data.results[0].daily[0].wind_speed+'km/h&nbsp;&nbsp;'+data.results[0].daily[0].wind_scale+'级</span></div><div class="ui-block-a"><span>明天</span><span>白天：'+data.results[0].daily[1].text_day+'&nbsp;<img src="img/weather/'+data.results[0].daily[1].code_day+'.png">&nbsp;&nbsp;最高气温：'+data.results[0].daily[1].high+'℃</span><span>夜间：'+data.results[0].daily[1].text_night+'&nbsp;<img src="img/weather/'+data.results[0].daily[1].code_night+'.png">&nbsp;&nbsp;最低气温：'+data.results[0].daily[1].low+'℃</span><span>'+data.results[0].daily[1].wind_direction+'风&nbsp;&nbsp;'+data.results[0].daily[1].wind_speed+'km/h&nbsp;&nbsp;'+data.results[0].daily[1].wind_scale+'级</span></div><div class="ui-block-a"><span>后天</span><span>白天：'+data.results[0].daily[2].text_day+'&nbsp;<img src="img/weather/'+data.results[0].daily[2].code_day+'.png">&nbsp;&nbsp;最高气温：'+data.results[0].daily[2].high+'℃</span><span>夜间：'+data.results[0].daily[2].text_night+'&nbsp;<img src="img/weather/'+data.results[0].daily[2].code_night+'.png">&nbsp;&nbsp;最低气温：'+data.results[0].daily[2].low+'℃</span><span>'+data.results[0].daily[2].wind_direction+'风&nbsp;&nbsp;'+data.results[0].daily[2].wind_speed+'km/h&nbsp;&nbsp;'+data.results[0].daily[2].wind_scale+'级</span></div></div>');
      },
      error: function() {
        $('.weather_content3').empty();
        $('.weather').css({
          'padding':'0px',
        })
        $('.weather_content1').append('<img src="img/error.png" style="width:100%">')
      }
    })
  }
  // 搜索城市
  $('.search_btn').on("tap",function(){
    var everycity = $('#search').val();
    weather(everycity);
    life(everycity);
    future(everycity);
  })
  // 热门城市
  $('.cities').on("tap",function(){
    var everycity = $(this).text();
    weather(everycity);
    life(everycity);
    future(everycity);
  })
  // 自动定位
  $('.citiies_gps').on("tap",function(){
    getLocation();
  })
  // 点击分类
  $("li").on("tap",function(){
    $('.news').empty();
    var news = $(this).children().attr('id');
    $.ajax({
      url: 'http://v.juhe.cn/toutiao/index?type='+news+'&key=9b965801da7dd6e63c02be6c1a99597b',
      // url:'json/news.json',
      type: 'get',
      dataType: 'json',
      cache: false,
      success:function(data){
        for(var i=0; i<data.result.data.length; i++) {
          $('.news').append('<div class="news_content" hrefs="'+data.result.data[i].url+'"><a href="#pageones" data-transition="slide"><img src="'+data.result.data[i].thumbnail_pic_s+'"/>'+'<div class=title>'+data.result.data[i].title+'<p>作者：'+data.result.data[i].author_name+'<br>'+data.result.data[i].date+'</p></div></a></div>')
        }
      },
      error: function() {
      }
    })
  });
  // 详细新闻
  $(document).on("tap",'.news_content',function(){
    $('#news').attr('src','');
    $('#news').attr('src',$(this).attr('hrefs'));
  })
})
